//
//  CycBaseTableViewCell.m
//  Cycling
//
//  Created by cjg on 2023/3/12.
//

#import "CycBaseTableViewCell.h"

@implementation CycBaseTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    
    if (highlighted) {
        self.backgroundColor = COLOR_WHITE;
    }else{
        self.backgroundColor = COLOR_WHITE;
    }
    [super setHighlighted:highlighted animated:animated];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = COLOR_APP_BACKGROUND;
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        self.line = [[UIView alloc] init];
        self.line.backgroundColor = COLOR_OFFLINE;
        [self.contentView addSubview:self.line];
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(MARGIN_MAX);
            make.right.bottom.equalTo(self.contentView).offset(0);
            make.height.mas_equalTo(1);
        }];
    }
    return self;
}

- (void)setFrame:(CGRect)frame{
    frame.origin.x += MARGIN_MAX;
//    frame.origin.y += MARGIN_MIDDLE/2;
//    frame.size.height -= MARGIN_MIDDLE;
    frame.size.width -= MARGIN_MAX * 2;
   [super setFrame:frame];
}

-(void)setOriginType:(CycBaseTableViewCellOriginType)originType {
    _originType = originType;
    [self mask];
}

- (void)mask {

    UIBezierPath *maskPath;
    if (self.originType == CycBaseTableViewCellOriginTypeTop) {
        maskPath = [UIBezierPath bezierPathWithRoundedRect:self.layer.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(DEFAULT_CORNER_RADIUS, DEFAULT_CORNER_RADIUS)];
    }
    if (self.originType == CycBaseTableViewCellOriginTypeBottom) {
        maskPath = [UIBezierPath bezierPathWithRoundedRect:self.layer.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(DEFAULT_CORNER_RADIUS, DEFAULT_CORNER_RADIUS)];
    }
    if (self.originType == CycBaseTableViewCellOriginTypeSingle) {
        maskPath = [UIBezierPath bezierPathWithRoundedRect:self.layer.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(DEFAULT_CORNER_RADIUS, DEFAULT_CORNER_RADIUS)];
    }
    if (maskPath) {
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }else {
        self.layer.mask = nil;
    }
}

- (void)setCurrentIndex:(NSUInteger)currentIndex cellCount:(NSUInteger)cellCount {
    if (cellCount == 1) {
        self.originType = CycBaseTableViewCellOriginTypeSingle;
        self.line.hidden = YES;
    }else {
        if (currentIndex == 0) {
            self.originType = CycBaseTableViewCellOriginTypeTop;
            self.line.hidden = NO;
        }else if (currentIndex == cellCount - 1) {
            self.originType = CycBaseTableViewCellOriginTypeBottom;
            self.line.hidden = YES;
        }else {
            self.originType = CycBaseTableViewCellOriginTypeCenter;
            self.line.hidden = NO;
        }
    }
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    [self mask];
}


@end
